package baiying_remote

import (
	"context"
	"encoding/json"
	"errors"
	"git.mycaigou.com/gfyx/common/simplecopier"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/common/utils/http_client"
)

const (
	// 创建任务 uri
	uriCreateTask = "/api/oauth/byai.openapi.calljob/1.0.0/create"
	// 修改任务
	uriUpdateTask = "/api/oauth/byai.openapi.calljob/1.0.0/update"
	// 删除任务
	uriDeleteTask = "/api/oauth/byai.openapi.calljob/1.0.0/delete"
	// 启动、暂停、终止任务
	uriExecuteTask = "/api/oauth/byai.openapi.calljob/1.0.0/execute"
	// 获取任务列表
	uriListTask = "/api/oauth/byai.openapi.calljob/1.0.0/list"
	// 批量获取任务状态
	uriGetBatchJobStatusList = "/api/oauth/byai.openapi.calljob.status/1.0.0/list"
	// 获取任务详情
	uriGetTaskDetail = "/api/oauth/byai.openapi.calljob.detail/1.0.0/get"
	// 获取任务详情中的未完成列表
	uriUnStartCallList = "/api/oauth/byai.openapi.calljob.callinstance.notstart/1.0.0/list"
	// 查询话术列表
	uriGetRobotList = "/api/oauth/byai.openapi.robot/1.0.0/list"
	// 获得公司AI坐席概况
	uriGetSeatInfoList = "/api/oauth/byai.openapi.seatinfo/1.0.0/get"
	// 获取公司外呼线路
	uriGetPhoneList = "/api/oauth/byai.openapi.phone/1.0.0/list"
	// 获取公司AI账户余额
	uriBalance = "/api/oauth/byai.openapi.company.ai/1.0.0/balance"
	// 客户导入
	uriCustomerImport = "/api/oauth/byai.openapi.calljob.customer/1.0.0/import"
)

type baiYingRepository struct {
}

func NewBaiYingRepository() BaiYingRepositoryIface {
	return &baiYingRepository{}
}

func (r *baiYingRepository) CreateTask(ctx context.Context, companyId int64, request *CreateTaskRequest) (*CreateTaskResponse, error) {
	paramMap, _ := simplecopier.StructToMapByJsonTag(request)
	response := &CreateTaskResponse{}
	_, _, errs := http_client.NewBaiYingHttpClient(companyId).Post(ctx, uriCreateTask, paramMap, nil).EndStruct(response)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	return response, nil
}

func (r *baiYingRepository) UpdateTask(ctx context.Context, companyId int64, request *UpdateTaskRequest) (*UpdateTaskResponse, error) {
	paramMap, _ := simplecopier.StructToMapByJsonTag(request)
	response := &UpdateTaskResponse{}
	_, _, errs := http_client.NewBaiYingHttpClient(companyId).Post(ctx, uriUpdateTask, paramMap, nil).EndStruct(response)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	return response, nil
}

func (r *baiYingRepository) DeleteTask(ctx context.Context, companyId int64, request *DeleteTaskRequest) (*DeleteTaskResponse, error) {
	paramMap, _ := simplecopier.StructToMapByJsonTag(request)
	response := &DeleteTaskResponse{}
	_, _, errs := http_client.NewBaiYingHttpClient(companyId).Post(ctx, uriDeleteTask, paramMap, nil).EndStruct(response)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	return response, nil
}

func (r *baiYingRepository) ExecuteTask(ctx context.Context, companyId int64, request *ExecuteTaskRequest) (*ExecuteTaskResponse, error) {
	paramMap, _ := simplecopier.StructToMapByJsonTag(request)
	response := &ExecuteTaskResponse{}
	_, _, errs := http_client.NewBaiYingHttpClient(companyId).Post(ctx, uriExecuteTask, paramMap, nil).EndStruct(response)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	return response, nil
}

func (r *baiYingRepository) CustomerImport(ctx context.Context, companyId int64, request *CustomerImportRequest) (*CustomerImportResponse, error) {
	paramMap, _ := simplecopier.StructToMapByJsonTag(request)
	listBytes, _ := json.Marshal(request.CustomerInfoVOList)
	paramMap["customerInfoVOList"] = string(listBytes)
	response := &CustomerImportResponse{}
	_, _, errs := http_client.NewBaiYingHttpClient(companyId).Post(ctx, uriCustomerImport, paramMap, map[string]string{"Content-Type": "application/x-www-form-urlencoded"}).EndStruct(response)
	if len(errs) > 0 {
		return nil, errs[0]
	}

	if response.Code != 200 {
		return nil, errors.New(response.ResultMsg)
	}

	return response, nil
}

func (r *baiYingRepository) GetRobotList(ctx context.Context, companyId int64, request *RobotListRequest) (*RobotListResponse, error) {
	paramMap, _ := simplecopier.StructToMapByJsonTag(request)
	response := &RobotListResponse{}
	_, _, errs := http_client.NewBaiYingHttpClient(companyId).Post(ctx, uriGetRobotList, paramMap, nil).EndStruct(response)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	return response, nil
}

func (r *baiYingRepository) GetPhoneList(ctx context.Context, companyId int64, request *PhoneListRequest) (*PhoneListResponse, error) {
	paramMap, _ := simplecopier.StructToMapByJsonTag(request)
	response := &PhoneListResponse{}
	_, _, errs := http_client.NewBaiYingHttpClient(companyId).Post(ctx, uriGetPhoneList, paramMap, nil).EndStruct(response)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	return response, nil
}

func (r *baiYingRepository) GetSeatInfoList(ctx context.Context, companyId int64, request *SeatInfoListRequest) (*SeatInfoListResponse, error) {
	paramMap, _ := simplecopier.StructToMapByJsonTag(request)
	response := &SeatInfoListResponse{}
	_, _, errs := http_client.NewBaiYingHttpClient(companyId).Post(ctx, uriGetSeatInfoList, paramMap, nil).EndStruct(response)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	return response, nil
}

// 获取公司AI账户余额 https://open.byai.com/document/balance_780
// 请求地址 https://open.byai.com/api/oauth/byai.openapi.company.ai/1.0.0/balance
func (r *baiYingRepository) GetBalance(ctx context.Context, companyId int64, request *GetBalanceRequest) (*GetBalanceResponse, error) {
	paramMap, _ := simplecopier.StructToMapByJsonTag(request)
	response := &GetBalanceResponse{}
	_, _, errs := http_client.NewBaiYingHttpClient(companyId).Post(ctx, uriBalance, paramMap, nil).EndStruct(response)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	return response, nil
}

func (r *baiYingRepository) GetTaskList(ctx context.Context, companyId int64, request *GetTaskListRequest) (*GetTaskListResponse, error) {
	// paramMap, _ := simplecopier.StructToMapByJsonTag(request)
	paramMap := map[string]interface{}{
		"companyId":  request.CompanyId,
		"createDate": request.CreateDate,
		"endDate":    request.EndDate,
		"jobName":    request.JobName,
		"pageNum":    request.PageNum,
		"pageSize":   request.PageSize,
	}
	if request.CallJobStatus > 0 {
		paramMap["callJobStatus"] = request.CallJobStatus
	}
	response := &GetTaskListResponse{}
	_, _, errs := http_client.NewBaiYingHttpClient(companyId).Post(ctx, uriListTask, paramMap, nil).EndStruct(response)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	return response, nil
}

func (r *baiYingRepository) GetTaskDetail(ctx context.Context, companyId int64, request *GetTaskDetailRequest) (*GetTaskDetailResponse, error) {
	paramMap, _ := simplecopier.StructToMapByJsonTag(request)
	response := &GetTaskDetailResponse{}
	_, _, errs := http_client.NewBaiYingHttpClient(companyId).Post(ctx, uriGetTaskDetail, paramMap, nil).EndStruct(response)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	return response, nil
}

func (r *baiYingRepository) GetUnStartCallList(ctx context.Context, companyId int64, request *GetUnStartCallListRequest) (*GetUnStartCallListResponse, error) {
	paramMap, _ := simplecopier.StructToMapByJsonTag(request)
	response := &GetUnStartCallListResponse{}
	_, _, errs := http_client.NewBaiYingHttpClient(companyId).Post(ctx, uriUnStartCallList, paramMap, nil).EndStruct(response)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	return response, nil
}

func (r *baiYingRepository) GetBatchJobStatusList(ctx context.Context, companyId int64, request *GetBatchJobStatusListRequest) (*GetBatchJobStatusListResponse, error) {
	paramMap, _ := simplecopier.StructToMapByJsonTag(request)
	response := &GetBatchJobStatusListResponse{}
	_, _, errs := http_client.NewBaiYingHttpClient(companyId).Post(ctx, uriGetBatchJobStatusList, paramMap, nil).EndStruct(response)
	if len(errs) > 0 {
		return nil, errs[0]
	}
	return response, nil
}
